﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormAutoTurnSet : Form
    {
        private OmronData plc => FormMainWindow.OmronPLC;

        private OmronData.Type自动开关.TypeOnOFF[] SwitchList;

        public FormAutoTurnSet()
        {
            InitializeComponent();

            SwitchList = new OmronData.Type自动开关.TypeOnOFF[]
            {
                plc.Z自动开关.CY除油,
                plc.Z自动开关.RS热水洗,
                plc.Z自动开关.SX水洗1,
                plc.Z自动开关.SX水洗2,
                plc.Z自动开关.WS微蚀,
                plc.Z自动开关.SX水洗3,
                plc.Z自动开关.SX水洗4,
                plc.Z自动开关.YJ预浸,
                plc.Z自动开关.SX水洗5,
                plc.Z自动开关.SX水洗6,
                plc.Z自动开关.SX水洗7,
                plc.Z自动开关.ZL整流机冷却,
                plc.Z自动开关.XG硝挂,
                plc.Z自动开关.XG硝挂后水洗1,
                plc.Z自动开关.XG硝挂后水洗2,
                plc.Z自动开关.SJR水加热循环泵,
                plc.Z自动开关.JT夹头吹干风车
            };
        }

        private void FormAutoTurnSet_Load(object sender, EventArgs e)
        {
            InitGrid(grid1);

            UpdateGrid(grid1);
        }

        public void InitGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;

            // 设置行数/列数
            grid.Rows = 18;
            grid.Cols = 9;

            for(int i=1; i < grid.Rows; i++)
            {
                grid.Cell(i, 3).Locked = true;
                grid.Cell(i, 6).Locked = true;
            }

            //这个包围区间要减1 居中
            grid.Range(0, 0, grid.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            for(int i=0; i< grid.Cols; i++)
            {
                grid.Cell(0, i).WrapText = true;
            }

            // 设置单元格文字
            int col = 0;
            grid.Cell(0, col++).Text = "名称";
            grid.Cell(0, col++).Text = "自动开功能\n(ON/OFF)";
            grid.Cell(0, col++).Text = "自动关功能\n(ON/OFF)";
            grid.Cell(0, col++).Text = "名称";
            grid.Cell(0, col++).Text = "自动开功能\n(ON/OFF)";
            grid.Cell(0, col++).Text = "自动关功能\n(ON/OFF)";
            grid.Cell(0, col++).Text = "名称";
            grid.Cell(0, col++).Text = "自动开功能\n(ON/OFF)";
            grid.Cell(0, col++).Text = "自动关功能\n(ON/OFF)";
    

            int row = 1;
            grid.Cell(row++, 0).Text = "酸性清洁";
            grid.Cell(row++, 0).Text = "热水洗";
            grid.Cell(row++, 0).Text = "水洗1";
            grid.Cell(row++, 0).Text = "水洗2";
            grid.Cell(row++, 0).Text = "微蚀";
            grid.Cell(row++, 0).Text = "水洗3";
            grid.Cell(row++, 0).Text = "水洗4";
            grid.Cell(row++, 0).Text = "预浸";
            grid.Cell(row++, 0).Text = "水洗5";
            grid.Cell(row++, 0).Text = "水洗6";
            grid.Cell(row++, 0).Text = "水洗7";
            grid.Cell(row++, 0).Text = "整流机冷却";
            grid.Cell(row++, 0).Text = "硝挂";
            grid.Cell(row++, 0).Text = "硝挂后水洗1";
            grid.Cell(row++, 0).Text = "硝挂后水洗2";
            grid.Cell(row++, 0).Text = "水加热循环泵";
            grid.Cell(row++, 0).Text = "夹头吹干风机";

            var bgc = Color.FromArgb(240, 240, 240);

            row = 1;
            for(int i = 1;i<=Config.镀铜槽数;i++)
            {
                grid.Cell(row, 3).BackColor = bgc;
                grid.Cell(row++, 3).Text = "铜"+ i.ToString()+"#喷流泵1";
                grid.Cell(row, 3).BackColor = bgc;
                grid.Cell(row++, 3).Text = "铜" + i.ToString() + "#喷流泵2";
            }

            grid.Column(1).CellType = FlexCell.CellTypeEnum.CheckBox;
            grid.Column(2).CellType = FlexCell.CellTypeEnum.CheckBox;
            grid.Column(4).CellType = FlexCell.CellTypeEnum.CheckBox;
            grid.Column(5).CellType = FlexCell.CellTypeEnum.CheckBox;
            grid.Column(7).CellType = FlexCell.CellTypeEnum.CheckBox;
            grid.Column(8).CellType = FlexCell.CellTypeEnum.CheckBox;

            //设置高宽
            grid.Row(0).Height = 60;
            grid.Column(0).Width = 120;
            grid.Column(1).Width = 80;
            grid.Column(2).Width = 80;
            grid.Column(3).Width = 80;
            grid.Column(4).Width = 80;
            grid.Column(5).Width = 80;
            grid.Column(6).Width = 80;
            grid.Column(7).Width = 80;
            grid.Column(8).Width = 80;
           

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        public void UpdateGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;

            for(int i=0; i< SwitchList.Length; i++)
            {
                int row = i + 1;

                grid.Cell(row, 1).Text = SwitchList[i].自动开.Value ? "1" : "0";
                grid.Cell(row, 2).Text = SwitchList[i].自动关.Value ? "1" : "0";

                grid.Cell(row, 1).ClearBackColor();
                grid.Cell(row, 2).ClearBackColor();
            }
            

            for(int i=0; i<Config.镀铜槽数; i++)
            {
                int row = 2*i + 1;

                var P1 = plc.Z自动开关.P喷流泵1;
                var P2 = plc.Z自动开关.P喷流泵2;

                grid.Cell(row, 4).Text = P1[i].自动开.Value ? "1" : "0";
                grid.Cell(row, 5).Text = P1[i].自动关.Value ? "1" : "0";

                grid.Cell(row+1, 4).Text = P2[i].自动开.Value ? "1" : "0";
                grid.Cell(row+1, 5).Text = P2[i].自动关.Value ? "1" : "0";

                grid.Cell(row, 4).ClearBackColor();
                grid.Cell(row, 5).ClearBackColor();
                grid.Cell(row + 1, 4).ClearBackColor();
                grid.Cell(row + 1, 5).ClearBackColor();
            }

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApply_Click(object sender, EventArgs e)
        {
            var gl = Global.GetInstance();
            if (!gl.CurrentUser.Check泵浦自动开关设定权限())
            {
                MessageBox.Show("您没有此权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            for (int i = 0; i < SwitchList.Length; i++)
            {
                int row = i + 1;

                SwitchList[i].自动开.Value = grid1.Cell(row, 1).BooleanValue;
                SwitchList[i].自动关.Value = grid1.Cell(row, 2).BooleanValue;
            }

            for (int i = 0; i < Config.镀铜槽数; i++)
            {
                int row = 2 * i + 1;

                var P1 = plc.Z自动开关.P喷流泵1;
                var P2 = plc.Z自动开关.P喷流泵2;

                P1[i].自动开.Value = grid1.Cell(row, 4).BooleanValue;
                P1[i].自动关.Value = grid1.Cell(row, 5).Text.ToBool();

                P2[i].自动开.Value = grid1.Cell(row + 1, 4).BooleanValue;
                P2[i].自动关.Value = grid1.Cell(row + 1, 5).BooleanValue;
            }

            UpdateGrid(grid1);
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }


        /// <summary>
        /// 全部选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdBtnAllON_CheckedChanged(object sender, EventArgs e)
        {
            grid1.AutoRedraw = false;  // 禁止刷新表格

            for (int i = 1;i<grid1.Rows;i++)
            {
                if (!string.IsNullOrWhiteSpace(grid1.Cell(i, 0).Text))
                {
                    grid1.Cell(i, 1).Text = "1";
                    grid1.Cell(i, 2).Text = "1";
                }

                if (!string.IsNullOrWhiteSpace(grid1.Cell(i, 3).Text))
                {
                    grid1.Cell(i, 4).Text = "1";
                    grid1.Cell(i, 5).Text = "1";
                }

                if (!string.IsNullOrWhiteSpace(grid1.Cell(i, 6).Text))
                {
                    grid1.Cell(i, 7).Text = "1";
                    grid1.Cell(i, 8).Text = "1";
                }
            }

            grid1.AutoRedraw = true;
            grid1.Refresh();
        }



        /// <summary>
        /// 全部取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdBtnAllOFF_CheckedChanged(object sender, EventArgs e)
        {
            grid1.AutoRedraw = false;  // 禁止刷新表格

            for (int i = 1; i < grid1.Rows; i++)
            {
                if (!string.IsNullOrWhiteSpace(grid1.Cell(i, 0).Text))
                {
                    grid1.Cell(i, 1).Text = "0";
                    grid1.Cell(i, 2).Text = "0";
                }

                if (!string.IsNullOrWhiteSpace(grid1.Cell(i, 3).Text))
                {
                    grid1.Cell(i, 4).Text = "0";
                    grid1.Cell(i, 5).Text = "0";
                }

                if (!string.IsNullOrWhiteSpace(grid1.Cell(i, 6).Text))
                {
                    grid1.Cell(i, 7).Text = "0";
                    grid1.Cell(i, 8).Text = "0";
                }
            }

            grid1.AutoRedraw = true;
            grid1.Refresh();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            UpdateGrid(grid1);
        }

        private void grid1_CellChange(object Sender, FlexCell.Grid.CellChangeEventArgs e)
        {
            int row = e.Row;
            int col = e.Col;

            if (row == 0) return;

            if (col == 0 || col == 3 || col == 6) return;

            grid1.Cell(row, col).BackColor = Color.GreenYellow;
        }
    }
}
